package com.jeecms.cms.entity.assist;

import com.jeecms.cms.entity.assist.Branch;
import com.jeecms.cms.entity.assist.base.BaseCmsEnterprise;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Set;

/**
 * Created by hashan on 2018-02-03.
 * 企业实体表
 */
public class CmsEnterprise extends BaseCmsEnterprise  {
    public JSONObject convertToJson(){
        JSONObject json = new JSONObject();
        if (getId() != null) {
            json.put("id", getId());
        } else {
            json.put("id", "");
        }
        if (StringUtils.isNotBlank(getName())) {
            json.put("name", getName());
        } else {
            json.put("name", "");
        }
        if (StringUtils.isNotBlank(getContacts())) {
            json.put("contacts", getContacts());
        } else {
            json.put("contacts", "");
        }
        if (getBranchs()!=null) {
            Set<Branch> set = getBranchs();
            JSONArray jsonArrayId = new JSONArray();
            JSONArray jsonArrayName = new JSONArray();
            JSONArray jsonArray = new JSONArray();
            int index = 0;
            for (Branch branch : set) {
                jsonArrayId.put(index,branch.getId());
                jsonArrayName.put(index,branch.getName());
                jsonArray.put(index,branch);
                index++;
            }
            json.put("branchIds", jsonArrayId);
            json.put("branchNames", jsonArrayName);
        }else{
            json.put("branchIds", new JSONArray());
            json.put("branchNames", new JSONArray());
        }
        if (StringUtils.isNotBlank(getInvitationCode())) {
            json.put("invitationCode", getInvitationCode());
            json.put("newInviteCode", getInvitationCode());
        } else {
            json.put("invitationCode", "");
            json.put("newInviteCode", "");
        }
        if (StringUtils.isNotBlank(getIndustry())){
            json.put("industry", getIndustry());
        }else{
            json.put("industry", "");
        }
        if (getInvitationCount() != null) {
            json.put("invitationCount", getInvitationCount());
        } else {
            json.put("invitationCount", "0");
        }
        if (StringUtils.isNotBlank(getLegalPerson())) {
            json.put("legalPerson", getLegalPerson());
        } else {
            json.put("legalPerson", "");
        }
        if (StringUtils.isNotBlank(getCompanyEmail())) {
            json.put("companyEmail", getCompanyEmail());
        } else {
            json.put("companyEmail", "");
        }
        if (StringUtils.isNotBlank(getCompanyId())) {
            json.put("companyId", getCompanyId());
        } else {
            json.put("companyId", "");
        }
        if (StringUtils.isNotBlank(getCompanyAddress())) {
            json.put("companyAddress", getCompanyAddress());
        } else {
            json.put("companyAddress", "");
        }
        if (StringUtils.isNotBlank(getPostCode())) {
            json.put("postCode", getPostCode());
        } else {
            json.put("postCode", "");
        }
        if (StringUtils.isNotBlank(getRemarks())) {
            json.put("remarks", getRemarks());
        } else {
            json.put("remarks", "");
        }
        return json;
    }

    public CmsEnterprise() {
        this.setInvitationCount(500);
    }

    public CmsEnterprise(Integer id) {
        super(id);
    }

    public CmsEnterprise(Integer id, String name, Set<Branch> branchs, String invitationCode, Integer invitationCount, String legalPerson, String phone, String companyEmail, String companyId, String postCode, String companyAddress, String remarks) {
        super(id, name, branchs, invitationCode, invitationCount, legalPerson, phone, companyEmail, companyId, postCode, companyAddress, remarks);
    }
}
